/*
 * @Author: your name
 * @Date: 2021-01-13 14:20:16
 * @LastEditTime: 2021-01-28 16:20:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wxapp\pages\vehicle\index\six-ex-inspect\six-ex-inspect.js
 */
var t = getApp(), e = t.requirejs("core"), a = t.requirejs("foxui");
Page({
  data: {
    imgUrls: ["https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2542054890,4291997804&fm=26&gp=0.jpg", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2863927798,667335035&fm=26&gp=0.jpg",],
    radio: "1",
    cate: {},
    order: { 'carseats': '0' },
    cateid: '',
    storeid: '',
    submit: 1,
    postData: {},
    requiredInfo: [
      {
        "imageSrc": "/static/images/icon.png",
        "title": '交强险保单',
        "subTitle": '有效期内',
      },
      {
        "imageSrc": "/static/images/icon.png",
        "title": '办理人身份证',
        "subTitle": '原件',
      },
      {
        "imageSrc": "/static/images/icon.png",
        "title": '行驶证',
        "subTitle": '原件',
      },
    ]

  },
  onLoad(options) {
    t.checkAuth();
    this.getDetail(options);
    this.setData({ cateid: options.id, storeid: options.storeid });
  },
  getDetail: function (t) {
    var a = this;
    e.get("vehicle/service/detail", {
      id: t.id
    }, function (t) {
      console.log('t.order.make_imgs', t.cate.make_imgs);

      a.setData({
        cate: t.cate
      })
      if (Object.keys(t.order).length > 0) {

        var picsList = [];
        picsList[0] = t.order.card_justimg == null ? [] : [{ url: t.order.card_justimg }];

        picsList[1] = t.order.card_returnimg == null ? [] : [{ url: t.order.card_returnimg }];

        picsList[2] = t.order.xsz_justimg == null ? [] : [{ url: t.order.xsz_justimg }];

        picsList[3] = t.order.xsz_returnimg == null ? [] : [{ url: t.order.xsz_returnimg }];

        picsList[4] = t.order.xsz_returnimg == null ? [] : [{ url: t.order.xsz_returnimg }];

        picsList[5] = t.order.insurance_img == null ? [] : [{ url: t.order.insurance_img }];

        console.log(picsList);
        a.setData({
          order: t.order,
          picsList: picsList
        })
      }

    })
  },
  onSubmit(b) {
    var uploadStatus = true;
    var that = this;
    var postData = this.selectComponent('.form-items').postData;
     //console.log('form-item component is ',postData);
    
    if (postData.platenumber == null) {
      e.toast('请输入车牌号码', "none")
      return;
    } else if (postData.framenumber == null) {
      e.toast('请输入车身架号', "none")
      return;
    } else if (postData.eniginenumber == null) {
      e.toast('请输入发动机号', "none")
      return;
    } else if (postData.ownersname == null) {
      e.toast('请输入车主姓名', "none")
      return;
    } else if (postData.registertime == null) {
      e.toast('车辆注册日期', "none")
      return;
    } else if (postData.location == null) {
      e.toast('请选择位置', "none")
      return;
    } else if (postData.address == null) {
      e.toast('请输入详细地址', "none")
      return;
    } else if (postData.phone == null) {
      e.toast('请输入联系电话', "none")
      return;
    } else if (postData.servicetime == null) {
      e.toast('请输入服务时间', "none")
      return;
    }else if (postData.card_justimg == null  && that.data.cate.make_imgs[0]==1) {
      e.toast('请上传身份证正面', "none")
      return;
    }else if (postData.card_returnimg == null && that.data.cate.make_imgs[1]==2) {
      e.toast('请上传身份证反面', "none")
      return;
    }else if (postData.xsz_justimg == null && that.data.cate.make_imgs[2]==3) {
      e.toast('请上传行驶证', "none")
      return;
    }else if (postData.xsz_returnimg == null && that.data.cate.make_imgs[3]==4) {
      e.toast('请上传行驶证副本', "none")
      return;
    }else if (postData.vehicle_img == null && that.data.cate.make_imgs[4]==5) {
      e.toast('请上传车辆图片', "none")
      return;
    }else if (postData.insurance_img == null && that.data.cate.make_imgs[5]==6) {
      e.toast('请上传交强险图片', "none")
      return;
    }

    if (that.data.submit == 0) {
      return false;
    }
    wx.showLoading({
      title: '正在提交材料',
      mask: true
    })
    that.setData({
      submit: 0
    })
    var o = e.getUrl("util/uploader/upload", {
      file: "file"
    });
    var arr = {
      'card_justimg': postData.card_justimg,
      'card_returnimg': postData.card_returnimg,
      'xsz_justimg': postData.xsz_justimg,
      'xsz_returnimg': postData.xsz_returnimg,
      'vehicle_img': postData.vehicle_img,
      'insurance_img': postData.insurance_img
    };
   
    postData.cateid = that.data.cateid;
    postData.storeid = that.data.storeid;
    postData.price = that.data.cate.price;
    var newarr = {};
    for (let k in arr) {
      if (arr[k] != undefined && arr[k].indexOf('car.tianjingcn.cn') == -1) {
        newarr[k] = arr[k];
      }
    }
    if (Object.keys(newarr).length > 0) {
      var num = 0;
      for (let key in newarr) {

        wx.uploadFile({
          url: o,
          filePath: newarr[key],
          name: "file",
          success: function (n) {
            var o = JSON.parse(n.data);
            if (0 != o.error) {
              e.toast("文件上传失败");
              wx.hideLoading();
              that.setData({
                submit: 1
              })
              return false;
            }
            postData[key] = o.files[0].url;
            num++;
            if (num == Object.keys(newarr).length) {
              e.post("vehicle/service/apply", { 'postData': postData }, function (t) {

                0 != t.error && wx.hideLoading(), wx.redirectTo({ url: "/pages/vehicle/order/pay/index?id=" + t.orderid });
              });
            }

          }
        });
      }
    } else {
      e.post("vehicle/service/apply", { 'postData': postData }, function (t) {
        0 != t.error && wx.hideLoading(), wx.redirectTo({ url: "/pages/vehicle/order/pay/index?id=" + t.orderid });
      });

    }

  }

})